/*
 * Copyright (c) 2019-2020, https://api.iot.10086.cn/(https://gitee.com/cmcc-iot-api/iot-card-open-application/).
 */
package cn.iot.card.open.terminal.controller;

import cn.iot.card.open.context.LoginContextHolder;
import cn.iot.card.open.context.model.LoginUser;
import cn.iot.card.open.exception.enums.BizExceptionEnum;
import cn.iot.card.open.modular.entity.AccountCard;
import cn.iot.card.open.modular.entity.Card;
import cn.iot.card.open.modular.entity.UserGroupRl;
import cn.iot.card.open.modular.log.ApiBussinessLog;
import cn.iot.card.open.modular.service.AccountCardService;
import cn.iot.card.open.modular.service.CardService;
import cn.iot.card.open.modular.service.UserService;
import cn.iot.card.open.pojo.page.LayuiPageFactory;
import cn.iot.card.open.pojo.page.LayuiPageInfo;
import cn.iot.card.open.terminal.dto.DownEmptySmsDTO;
import cn.iot.card.open.terminal.dto.DownTemplateSmsDTO;
import cn.iot.card.open.terminal.dto.UpAndDownSmsRecordsDTO;
import cn.iot.card.open.terminal.service.UpOrDownSmsService;
import cn.iot.card.open.util.CommonUtil;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 上/下行短信
 *
 * @author zhanlele
 * @date 20200225 14:49:04
 * @modify 20200225 zhanlele v1.1 创建
 * @since v1.1
 */
@Controller
@RequestMapping("/terminalCtrl")
public class UpOrDownSmsController {

    /**
     * 上/下行短信页面路径地址前缀
     */
    private static String PREFIX = "/modular/terminal";

    @Autowired
    private CardService cardService;

    @Autowired
    private UserService userService;

    @Autowired
    private AccountCardService accountCardService;

    @Autowired
    private UpOrDownSmsService upOrDownSmsService;

    /**
     * 跳转上/下行短信主页面
     *
     * @return 返回 string
     * @author zhanlele
     * @date 20200225 14:49:35
     * @modify 20200225 zhanlele v1.1 创建
     * @since v1.1
     */
    @RequestMapping("/upOrDownSms")
    public String upOrDownSms() {
        return PREFIX + "/upOrDownSms.html";
    }

    /**
     * 查询上/下行短信记录
     *
     * @param upAndDownSmsRecordsDTO
     * @return 返回 layui page info
     * @author zhanlele
     * @date 20200225 14:50:25
     * @modify 20200225 zhanlele v1.1 创建
     * @since v1.1
     */
    @RequestMapping("/upAndDownSmsRecords")
    @ResponseBody
    public LayuiPageInfo upAndDownSmsRecords(UpAndDownSmsRecordsDTO upAndDownSmsRecordsDTO) throws Exception {
        String msisdn = upAndDownSmsRecordsDTO.getMsisdn();
        String iccid = upAndDownSmsRecordsDTO.getIccid();
        String imsi = upAndDownSmsRecordsDTO.getImsi();
        if (ToolUtil.isEmpty(upAndDownSmsRecordsDTO.getStartDate())) {
            return LayuiPageFactory.createEmptyPageInfo();
        }

        Card card = cardService.verifyCardInfo(msisdn, iccid, imsi);
        if (null == card) {
            return LayuiPageFactory.createBizExceptionMsg(BizExceptionEnum.CARD_NOT_EXIT_IN_SYSTEM);
        }

        LoginUser loginUser = LoginContextHolder.getContext().getUser();
        UserGroupRl userGroupRl = userService.getUserGroupRlByUserAndCard(loginUser.getId(), card.getCardId());
        if (null == userGroupRl) {
            return LayuiPageFactory.createBizExceptionMsg(BizExceptionEnum.CARD_NOT_BELONG_USER);
        }

        AccountCard accountCard = accountCardService.getBaseMapper().findByCardId(card.getCardId());
        String accountId = String.valueOf(accountCard.getAccountId());
        String transId = CommonUtil.generateTransId(accountId);

        return upOrDownSmsService.selectUpAndDownSmsRecord(transId, accountId, upAndDownSmsRecordsDTO);
    }

    /**
     * 跳转发送下行模板短信页面
     *
     * @return 返回 string
     * @author zhanlele
     * @date 20200225 21:12:45
     * @modify 20200225 zhanlele v1.1 创建
     * @since v1.1
     */
    @RequestMapping("/toSendDownTemplateSmsPage")
    public String toSendDownTemplateSmsPage() {
        return PREFIX + "/sendDownTemplateSms.html";
    }

    /**
     * 发送下行模板短信
     *
     * @return 返回 response data
     * @author zhanlele
     * @date 20200226 09:37:05
     * @modify 20200226 zhanlele v1.1 创建
     * @since v1.1
     */
    @RequestMapping("/sendDownTemplateSms")
    @ApiBussinessLog(value = "发送下行模板短信")
    @ResponseBody
    public ResponseData sendDownTemplateSms(DownTemplateSmsDTO downTemplateSmsDTO) throws Exception {
        String msisdns = downTemplateSmsDTO.getMsisdns();
        Card card = cardService.verifyCardInfo(msisdns, null, null);
        if (null == card) {
            throw new ServiceException(BizExceptionEnum.CARD_NOT_EXIT_IN_SYSTEM);
        }

        LoginUser loginUser = LoginContextHolder.getContext().getUser();
        UserGroupRl userGroupRl = userService.getUserGroupRlByUserAndCard(loginUser.getId(), card.getCardId());
        if (null == userGroupRl) {
            throw new ServiceException(BizExceptionEnum.CARD_NOT_BELONG_USER);
        }

        AccountCard accountCard = accountCardService.getBaseMapper().findByCardId(card.getCardId());
        String accountId = String.valueOf(accountCard.getAccountId());
        String transId = CommonUtil.generateTransId(accountId);
        String actionid = downTemplateSmsDTO.getActionid();
        String params = downTemplateSmsDTO.getParams();
        String encode = downTemplateSmsDTO.getEncode();
        return upOrDownSmsService.sendDownTemplateSms(transId, accountId,
                msisdns, actionid, params, encode);
    }

    /**
     * 跳转发送下行空短信页面
     *
     * @return 返回 string
     * @author zhanlele
     * @date 20200225 21:41:01
     * @modify 20200225 zhanlele v1.1 创建
     * @since v1.1
     */
    @RequestMapping("/toSendDownBlankSmsPage")
    public String toSendDownBlankSmsPage() {
        return PREFIX + "/sendDownBlankSms.html";
    }

    /**
     * 发送下行空短信
     *
     * @return 返回 response data
     * @author zhanlele
     * @date 20200226 09:38:07
     * @modify 20200226 zhanlele v1.1 创建
     * @since v1.1
     */
    @RequestMapping("/sendDownBlankSms")
    @ApiBussinessLog(value = "发送下行空短信")
    @ResponseBody
    public ResponseData sendDownBlankSms(DownEmptySmsDTO downEmptySmsDTO) throws Exception {
        String msisdns = downEmptySmsDTO.getMsisdns();
        Card card = cardService.verifyCardInfo(msisdns, null, null);
        if (null == card) {
            throw new ServiceException(BizExceptionEnum.CARD_NOT_EXIT_IN_SYSTEM);
        }

        LoginUser loginUser = LoginContextHolder.getContext().getUser();
        UserGroupRl userGroupRl = userService.getUserGroupRlByUserAndCard(loginUser.getId(), card.getCardId());
        if (null == userGroupRl) {
            throw new ServiceException(BizExceptionEnum.CARD_NOT_BELONG_USER);
        }

        AccountCard accountCard = accountCardService.getBaseMapper().findByCardId(card.getCardId());
        String accountId = String.valueOf(accountCard.getAccountId());
        String transId = CommonUtil.generateTransId(accountId);

        return upOrDownSmsService.sendDownBlankSms(transId, accountId, msisdns, downEmptySmsDTO.getValidTime());
    }
}
